"""
The MIT License (MIT)
Copyright © 2021 Walkline Wang (https://walkline.wang)
Gitee: https://gitee.com/walkline/esp32-ble-beacon_locating
"""

class Utilities(object):
	@staticmethod
	def get_chip_id():
		from machine import unique_id

		return "".join(['%02X' % i for i in unique_id()])

	@staticmethod
	def is_mode_file_exist():
		"""
		如果 mode 文件存在，则表示启用信标模式
		"""
		import os

		try:
			os.stat("mode")

			return True
		except OSError:
			return False

	@staticmethod
	def create_or_delete_mode_file():
		result = False

		if Utilities.is_mode_file_exist():
			result = Utilities.__del_mode_file()
		else:
			result = Utilities.__create_mode_file()
		
		return result

	@staticmethod
	def __create_mode_file():
		try:
			with open("mode", "w") as file:
				pass

			return True
		except OSError:
			return False

	@staticmethod
	def __del_mode_file():
		import os

		try:
			os.remove("mode")

			return True
		except OSError:
			return False

	@staticmethod
	def soft_reset():
		from sys import exit
		exit()

	@staticmethod
	def hard_reset():
		from machine import reset
		reset()

	@staticmethod
	def is_esp32c3():
		from machine import Pin
		from os import uname

		if uname()[0] == 'esp32':
			try:
				Pin(22, Pin.OUT)
				return False
			except ValueError:
				return True
